package com.firstgroup.controller;

import com.firstgroup.Utils.ResponseResult;
import com.firstgroup.domain.Comment;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.repository.InterviewersRepository;
import com.firstgroup.repository.UserRepository;
import com.firstgroup.service.InterViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Controller
@Slf4j
@Api(tags = "面试人员操作")
public class InterviewersController {

    @Resource
    InterViewService interViewService;
    private Logger logger = LoggerFactory.getLogger(InterviewersController.class);
    @Resource
    InterviewersRepository interviewersRepository;

    @Resource
    UserRepository userRepository;


    @ApiOperation("面试人员报名")
    @ResponseBody
    @PostMapping("/register")
    public ResponseResult register(Interviewers interviewers) {
        Interviewers interviewers1 = interviewersRepository.findInterviewersByStudentId(interviewers.getStudentId());
        log.info(""+interviewers.getStudentId());
        log.info("noeTime{}",LocalDateTime.now().toString());
        if (interviewers1 != null) {
            Comment comment = userRepository.findBanUserByInterViewId(interviewers1.getInterviewersId());
            if (comment!=null){
                return ResponseResult.error(comment.getContent());
            }
            //设置日期格式，和录入数据库的日期格式相同
            DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            long day = LocalDate.parse(LocalDateTime.now().format(dateFormat),dateFormat).toEpochDay()-
                        LocalDate.parse(interviewers1.getRegistTime(),dateFormat).toEpochDay();
            if(day<100){
                return ResponseResult.error("您已经报过名了,时长未满100天，请勿重复报名");
            }
        }
        if (interviewers.getStudentId().length() != 11) {
            return ResponseResult.error("学号格式不正确");
        }
        if (interviewers.getTelephone().length() != 11) {
            return ResponseResult.error("手机号码格式不正确");
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date1=  simpleDateFormat.format(date);
        interviewers.setRegistTime(date1);
        interviewersRepository.save(interviewers);
        return ResponseResult.success("报名成功", interviewers);
    }

    @PostMapping("/upLoad")
    @ResponseBody
    @ApiOperation("上传简历正反面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path",required = true,value = "0表示上传正面，1上传反面，输入其他无效"),
            @ApiImplicitParam(name = "interviewId",required = true,value = "面试人员ID")
    })
    public ResponseResult upLoad(@RequestParam("file") MultipartFile file,
                                 @RequestParam("studentId") Integer interviewId,
                                 @RequestParam("path") String path) {
        final String[] PATHS = {"0", "1"};
        boolean flag=true;
        for (String paths : PATHS) {
            if (path.equals(paths)) {
                flag=false;
                break;
            }
        }
        if (flag){
            return ResponseResult.error("错误的操作");
        }
        Interviewers interviewers= interviewersRepository.findByInterviewersId(interviewId);
        if (interviewers == null) {
            return ResponseResult.error("找不到该用户");
        }
        return interViewService.upLoad(file, interviewId, path);
    }

    @ApiOperation(value = "删除面试者")
    @RequestMapping(value = "/deleteInterviewer",method = RequestMethod.DELETE)
    @ResponseBody
    public Interviewers deleteInterviewer(Integer id){
     Interviewers interviewers=   interViewService.findByInterviewersId(id);
        interViewService.deleteById(id);
            return interviewers;
    }

    @ApiOperation(value = " 删除面试者")
    @RequestMapping(value = "/batchDeleteInterviewer",method = RequestMethod.DELETE)
    @ResponseBody
    public int deleteInterviewers(Integer[] id){
        try {
            return interViewService.batchDeleteInterviewers(id);
        } catch (Exception e) {
            logger.info("error",e);
        }
        return -1;
    }

    @ApiOperation(value = "根据状态查找面试者")
    @RequestMapping(value = "/findInterviewersByState",method = RequestMethod.GET)
    @ResponseBody
    public List<Interviewers> findInterviewersByState(String state){
        return interViewService.findInterviewersByState(state);
}

    @ApiOperation(value = "根据名字模糊查询面试者")
    @RequestMapping(value = "/findByNameLike",method = RequestMethod.GET)
    @ResponseBody
    public List<Interviewers> findByNameLike(String name){
        List<Interviewers> list=interViewService.findByNameLike("%"+name+"%");
        System.out.println(list);
        if(list.isEmpty()){
            return null;

        }else {
            return list;
        }
    }
    @ApiOperation(value = "根据ID更新面试人员的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "面试人员的ID",required = true),
            @ApiImplicitParam(name = "name",value = "姓名",required = true),
            @ApiImplicitParam(name = "qq",value = "QQ号码",required = true),
            @ApiImplicitParam(name = "studentId",value = "学号",required = true),
            @ApiImplicitParam(name = "classes",value = "班级",required = true),
            @ApiImplicitParam(name = "sex",value = "性别",required = true),
            @ApiImplicitParam(name = "telephone",value = "电话号码",required = true)
    })
    @PostMapping("/updateInterviewer")
    @ResponseBody
    public ResponseResult updateInterviewer(Integer id,String name, String qq, String studentId, String classes, String telephone, String sex){
        String msg="";
//        if (studentId.length() != 11) {
//            msg="请输入正确的学号";
//            return ResponseResult.error(msg);
//        }
        if (telephone.length() != 11) {
            msg="手机号格式不正确";
            return ResponseResult.error(msg);
        }
        boolean flag=true;
        if ("男".equals(sex)) {
            flag=false;
        }
        if (flag && "女".equals(sex)){
            flag=false;
        }
        if (flag){
            return ResponseResult.error("请输入正确的性别");
        }
        return interViewService.updateInterviews(id,name,qq,studentId,classes,telephone,sex);
    }


    @ResponseBody
    @RequestMapping(value = "/updateTimeAndPlace",method = RequestMethod.PUT)
    @ApiOperation(value = "修改时间地点")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "状态",name = "state",required = true,type = "String"),
            @ApiImplicitParam(value = "时间",name = "time",required = true,type = "String"),
            @ApiImplicitParam(value = "地点",name = "place",required = true,type = "String")

    })
    public int updateTimeAndPlace(String state,String time,String place){
   int p=    interViewService.updateTimeAndPlace(state, time, place);
       Interviewers interviewers=new Interviewers();
       interviewers.setPlace(place);
       interviewers.setTime(time);
        return p;

    }

    @ResponseBody
    @RequestMapping(value = "/saveUsers",method = RequestMethod.PUT)
    @ApiOperation(value = "保存面试官")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "面试官名字",name = "user",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者id",name = "id",required = true,type = "int")
    })
    public Interviewers saveUsers(String user, int id){
        interViewService.saveUsers(user, id);
        Interviewers interviewers=new Interviewers();
        interviewers.setUser(user);
        return interviewers;
    }


    @ApiOperation(value = "根据面试官名字模糊查询面试者信息")
    @RequestMapping(value = "/findByUserLike",method = RequestMethod.GET)
    @ResponseBody
    public List<Interviewers> findByUserLike(String user){
        List<Interviewers> list=interViewService.findByUserLike("%"+user+"%");
        return list;
    }
    @RequestMapping(value = "/addInterviewers",method = RequestMethod.POST)
    @ApiOperation(value = "添加面试者功能")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "面试者姓名",name = "name",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者班级",name = "classes",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者学号",name = "studentId",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者qq",name = "qq",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者电话",name = "Telephone",required = true,type = "String"),
            @ApiImplicitParam(value = "面试者性别",name = "sex",required = true,type = "String")

    })
    public ResponseResult addInterviewers(String name, String classes, String studentId, String qq, String Telephone, String sex){
//        Interviewers interviewers1=interviewersRepository.findByStudentId(studentId);
//        if(interviewers1!=null){
//            return ResponseResult.error("你已经报过名了");
//        }
        if(studentId.length()!=11){
            return ResponseResult.error("请输入正确的学号");
        }
        if(Telephone.length()!=11){
            return ResponseResult.error("请输入正确手机号码");
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date1=  simpleDateFormat.format(date);
       Interviewers interviewers=new Interviewers();
       interviewers.setName(name);
       interviewers.setClasses(classes);
       interviewers.setTelephone(Telephone);
       interviewers.setStudentId(studentId);
       interviewers.setQq(qq);
       interviewers.setSex(sex);
       interviewers.setRegistTime(date1);
        interViewService.addInterviewers(interviewers);
        return ResponseResult.success(interviewers);
    }
    @ResponseBody
    @GetMapping("/findByYear")
    @ApiOperation(value = "根据年遍历面试者")
    public ResponseResult findByYear(String year){
        return interViewService.findByYear(year);
    }

    @ResponseBody
    @GetMapping("/findRepeatResister")
    @ApiOperation(value = "遍历重复报名者")
    public ResponseResult findRepeatResister(){
        return interViewService.findRepeatResister();
    }

    @ResponseBody
    @GetMapping("/findInterviewersByStudentId")
    @ApiOperation(value = "遍历重复报名者--具体情况-根据学号查询某个人")
    public ResponseResult findInterviewersByStudentId(String studentId){
        return interViewService.findInterviewersByStudentId(studentId);
    }
}
